package inet.app.vod.adapter;

import inet.app.vod.activity.R;
import inet.app.vod.object.MenuUIItem;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends ArrayAdapter<MenuUIItem> {

	List<MenuUIItem> mModels;
	Context mContext;
	LayoutInflater mInflater;
	int mCurrentItem;

	public MenuAdapter(Context context, int textViewResourceId,
			List<MenuUIItem> models, int currentItem) {

		super(context, textViewResourceId);
		this.mModels = models;
		this.mContext = context;
		this.mInflater = LayoutInflater.from(mContext);
		this.mCurrentItem = currentItem;
	}

	@Override
	public int getCount() {
		return this.mModels.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		final MenuUIItem model = mModels.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_menu, null);
			holder = new ViewHolder();
			holder.mSelectView = convertView.findViewById(R.id.select_view);
			holder.mLogo = (ImageView) convertView.findViewById(R.id.logo_img);
			holder.mNameMenu = (TextView) convertView
					.findViewById(R.id.name_menu);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mSelectView.setVisibility(model.isSelect() ? View.VISIBLE
				: View.INVISIBLE);
		holder.mLogo.setBackgroundResource(model.getResId());
		holder.mNameMenu.setText(model.getName());
		return convertView;
	}

	static class ViewHolder {
		public View mSelectView;
		public ImageView mLogo;
		public TextView mNameMenu;
	}

}
